package org.rabbitmq.rabbitextensions.listener;

import com.rabbitmq.client.Channel;
import org.rabbitmq.rabbitextensions.constant.Constants;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Date;

//@Component
public class DLListener {
//    //死信消息监听
//    @RabbitListener(queues = Constants.NORMAL_QUEUE)
//    public void handlerMessage(Message message, Channel channel) throws IOException {
//        long  deliveryTag = message.getMessageProperties().getDeliveryTag();
//        try{
//            System.out.printf("[norm]收到消息:%s ,deliverTag: %d \n",new String(message.getBody(),"UTF-8"),
//                    message.getMessageProperties().getDeliveryTag());
//            System.out.println("处理业务逻辑");
////            int t = 1/0;
//            System.out.println("处理完成");
//            //肯定确认                      是否批量确认
//            channel.basicAck(deliveryTag,false);
//        }catch(Exception e){
//            //否定确认:                      是否批量确认  不再重新入队
//            channel.basicNack(deliveryTag,false,false);
//        }
//    }

    @RabbitListener(queues = Constants.DL_QUEUE)
    public void handlerMessage2(Message message, Channel channel) throws IOException {
        System.out.printf("%tc 死信队列接收到消息: %s, deliveryTag: %d%n", new
                        Date(), new String(message.getBody(),"UTF-8"),
                message.getMessageProperties().getDeliveryTag());
    }
}
